#include "connector.h"
#include "connectorlist.h"
Connector::Connector(QTcpSocket* socket,QThread* thread):m_socket(socket),m_thread(thread)
{
    m_socketName = QString("%1:%2")
                       .arg(socket->peerAddress().toString())
                       .arg(socket->peerPort());
}

void Connector::readData()
{
    qDebug() << "thread:" << QThread::currentThreadId() << " receive msg";
    m_socket->write("哈哈哈");
    m_socket->flush();
}

void Connector::disconnected()
{
    qDebug() << "thread:" << QThread::currentThreadId() << " disconnected";
    if(m_socket != NULL)
    {
        delete m_socket;
        m_socket = NULL;
    }
}

void Connector::stateChanged(QAbstractSocket::SocketState state)
{
    switch (state) {
    case QAbstractSocket::UnconnectedState:
        qDebug() << "case QAbstractSocket::UnconnectedState:";
        if(m_socket != NULL)
        {
            delete m_socket;
            m_socket = NULL;
        }
        if(m_thread->isRunning())
        {
            m_thread->exit();
        }

        ConnectorList::deleteClient(this->m_socketName);
        break;
    case QAbstractSocket::HostLookupState:
        break;
    case QAbstractSocket::ConnectingState:
        break;
    case QAbstractSocket::ConnectedState:
        break;
    case QAbstractSocket::BoundState:
        break;
    case QAbstractSocket::ListeningState:
        break;
    case QAbstractSocket::ClosingState:
        break;
    default:
        break;
    }
}
